package com.achobeta.recruitment.service.impl.manager;

import com.achobeta.recruitment.common.ResultCode;
import com.achobeta.recruitment.dao.manager.ManageRemarksMapper;
import com.achobeta.recruitment.model.ManageRemarks;
import com.achobeta.recruitment.model.Student;
import com.achobeta.recruitment.service.manager.ManageRemarksService;
import com.achobeta.recruitment.service.student.StudentService;
import com.achobeta.recruitment.utils.CheckUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-30
 * Time: 22:26
 */
@Service
@Slf4j
public class ManageRemarksServiceImpl implements ManageRemarksService {

    @Autowired
    private ManageRemarksMapper manageRemarksMapper;

    @Autowired
    private StudentService studentService;


    @Override
    public Integer initManageRemarks() {
        // 构造对象
        ManageRemarks remarks = new ManageRemarks();
        remarks.setRemark("");
        remarks.setArrangement("");
        remarks.setConclusion("");
        Date date = new Date();
        remarks.setCreateTime(date);
        remarks.setUpdateTime(date);
        remarks.setDeleteState((byte) 0);
        // 插入数据库
        int rows = manageRemarksMapper.insertSelective(remarks);
        // 校验
        CheckUtils.checkRows(rows, 1, ResultCode.FAILED_CREATE);
        // 成功
        log.info(ResultCode.SUCCESS + " remarks id：" + remarks.getId());
        return remarks.getId();
    }

    @Override
    public void saveManageRemarks(String openid, ManageRemarks remarks) {
        // 校验
        CheckUtils.checkNULL(remarks);
        CheckUtils.checkStringParams(ResultCode.FAILED_PARAMS_OPENID, openid);
        // 查询学生表
        Student student = studentService.selectStudentByOpenid(openid);
        CheckUtils.checkNULL(student);
        Integer manageRemarkId = student.getManageRemarkId();
        CheckUtils.checkNULL(manageRemarkId);
        remarks.setId(manageRemarkId);
        // 调用dao层
        int rows = manageRemarksMapper.updateByPrimaryKeySelective(remarks);
        // 校验
        CheckUtils.checkRows(rows, 1, ResultCode.FAILED_MANAGE_REMARKS_UPDATE);
        // 成功
        log.info(ResultCode.SUCCESS + "  remarks id：" + remarks.getId());
    }

}
